(function() {
	/**
	 * YUI Library aliases
	 */
	var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;

	/**
	 * Alfresco Slingshot aliases
	 */
	var $html = Alfresco.util.encodeHTML;

	Alfresco.YouTube = function(htmlId) {
		this.name = "Alfresco.YouTube";
		this.id = htmlId;
		this.configDialog = null;

		/**
		 * Register this component
		 */
		Alfresco.util.ComponentManager.register(this);

		/**
		 * Load YUI Components
		 */
		Alfresco.util.YUILoaderHelper.require( [], this.onComponentsLoaded, this);

		return this;
	};

	YAHOO.extend(Alfresco.YouTube, Alfresco.component.Base, {
		/**
		 * Object container for initialization options
		 * 
		 * @property options
		 * @type object
		 */
		options : {
			/**
			 * The component id
			 * 
			 * @property componentId
			 * @type string
			 * @default ""
			 */
			componentId : "",

			/**
			 * The configurable title to display
			 * 
			 * @property titleStr
			 * @type string
			 * @default ""
			 */
			titleStr : "",

			/**
			 * The configurable ytUrl to display
			 * 
			 * @property ytUrlStr
			 * @type string
			 * @default ""
			 */
			ytUrlStr : "",

			/**
			 * The configurable start to display
			 * 
			 * @property startStr
			 * @type string
			 * @default ""
			 */
			startStr : "",			
		},

		/**
		 * Fired by YUI when parent element is available for scripting.
		 * Component initialisation, including instantiation of YUI widgets and
		 * event listener binding.
		 * 
		 * @method onReady
		 */
		onReady : function A_onReady() {
			// Add click handler to config YouTube link that will be visible if
			// user
			// is site manager.
			var configFeedLink = Dom.get(this.id + "-configYouTube-link");
			if (configFeedLink) {
				Event.addListener(configFeedLink, "click", this.onConfigYouTubeClick, this, true);
			}
		},

		/**
		 * Called when the user clicks the config YouTube link. Will open an YouTube
		 * config dialog
		 * 
		 * @method onConfigYouTubeClick
		 * @param e
		 *            The click event
		 */
		onConfigYouTubeClick : function A_onConfigYouTubeClick(e) {
			var actionUrl = Alfresco.constants.URL_SERVICECONTEXT + "modules/youtube/config/"
					+ encodeURIComponent(this.options.componentId);
			
			Event.stopEvent(e);

			if (!this.configDialog) {
				this.configDialog = new Alfresco.module.SimpleDialog(this.id + "-configDialog").setOptions( {
					width : "30em",
					templateUrl : Alfresco.constants.URL_SERVICECONTEXT + "modules/youtube/config",
					actionUrl : actionUrl,
					onSuccess : {
						fn : function YouTube_onConfigFeed_callback(response) {
							var youtube = response.json;

							// Save values for new config dialog openings
							this.options.titleStr = (youtube && youtube.title) ? youtube.title : this.options.titleStr;
							this.options.ytUrlStr = (youtube && youtube.ytUrl) ? youtube.ytUrl : this.options.ytUrlStr;
							this.options.startStr = (youtube && youtube.start) ? youtube.start : this.options.startStr;

							// Update dashlet body with new values
							// window.location.reload(true);
							if (YAHOO.env.ua.ie === 6) {
								window.location.reload(true);
							} else {
								Dom.get(this.id + "-YouTube").src = youtube ? youtube.ytUrl : this.options.ytUrlStr;
								Dom.get(this.id + "-title").innerHTML = youtube ? youtube.title : this.options.titleStr;
							}
						},
						scope : this
					},
					doSetupFormsValidation : {
						fn : function YouTube_doSetupForm_callback(form) {
							form.addValidation(this.configDialog.id + "-ytUrl", Alfresco.forms.validation.mandatory,
									null, "blur");
							form.addValidation(this.configDialog.id + "-ytUrl", Alfresco.forms.validation.mandatory,
									null, "keyup");
							form.addValidation(this.configDialog.id + "-ytUrl", Alfresco.forms.validation.url,
									null, "keyup");
							form.addValidation(this.configDialog.id + "-ytUrl", Alfresco.forms.validation.regexMatch,
									{pattern: /^(http|https):\/\/www.youtube.com\/(watch\?v=|embed\/)/}, "keyup");
							form.addValidation(this.configDialog.id + "-start", Alfresco.forms.validation.number, null,
									"keyup");
							form.setShowSubmitStateDynamically(true, false);

							Dom.get(this.configDialog.id + "-title").value = this.options.titleStr;
							Dom.get(this.configDialog.id + "-ytUrl").value = this.options.ytUrlStr;
							Dom.get(this.configDialog.id + "-start").value = this.options.startStr;
						},
						scope : this
					}
				});
			} else {
				this.configDialog.setOptions( {
					actionUrl : actionUrl
				});
			}
			this.configDialog.show();
		}
	});

})();